package top.lidee.module.backendApi.twice.third.ali;

import cn.hutool.core.exceptions.StatefulException;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.AccountInfo;
import top.lidee.common.service.twice.management.MtStoreAlipayInputPieceLogService;
import top.lidee.common.service.twice.third.ali.ZftSimpleCreateService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.pojo.management.MtStoreAlipayInputPieceLogEntity;

import javax.servlet.http.HttpServletRequest;
import java.io.Reader;
import java.util.Map;
import java.util.Objects;

import static top.lidee.framework.constant.AlipayDefaultSettleTypeCon.INCOMING_ITEMS_ARE_ABNORMAL;
import static top.lidee.framework.constant.AlipayDefaultSettleTypeCon.THE_INCOMING_REVIEW_IS_COMPLETE;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 支付宝收款 互联网平台支付通
 * @date 2024/9/26 12:40
 */
@Api(tags = "支付宝收款 互联网平台支付通")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/aliPay/zft")
public class AliPayZftController {

    /**
     * 直付通/服务商二级商户进件
     */
    private final ZftSimpleCreateService zftSimpleCreateService;

    private final MtStoreAlipayInputPieceLogService mtStoreAlipayInputPieceLogService;

    /**
     * 直付通/服务商二级商户进件
     * storeId:店铺id
     */
    @RequestMapping(value = "/create/{storeId}", method = RequestMethod.GET)
    @ApiOperation(value = "直付通/服务商二级商户标准进件 ")
    public ResponseResult simplecreate(@PathVariable("storeId") Integer storeId,
                                       HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = zftSimpleCreateService.create(storeId, accountInfo);
        return responseResult;
    }

    /**
     * ant.merchant.expand.indirect.zftorder.query(直付通/服务商二级商户入驻进度查询)
     * 申请单id。通过 ant.merchant.expand.indirect.zft.create(直付通二级商户创建)接口返回。与 external_id 二选一必填，若同时传入将以 order_id 为准进行查询。
     */
    @RequestMapping(value = "/progressInquiry", method = RequestMethod.GET)
    @ApiOperation(value = "直付通/服务商二级商户入驻进度查询")
    public ResponseResult progressInquiry(@RequestParam(value = "orderId",required = false) String orderId,
                                          @RequestParam(value = "externalId",required = false) String externalId) {

        return zftSimpleCreateService.progressInquiry(orderId,externalId);
    }


    //<editor-fold desc="支付宝回调">
    /**
     * ant.merchant.expand.indirect.zft.passed(直付通/服务商二级商户进件审核通过消息)
     * notify_id	通知ID
     * utc_timestamp 消息发送时的服务端时间
     * msg_method	消息接口名称
     * app_id	消息接受方的应用id
     * msg_type	消息类型。目前支持类型：sys：系统消息；usr，用户消息；app，应用消息
     * msg_app_id 消息归属方的应用id。应用消息时非空
     * version	 版本号(1.1版本为标准消息)
     * biz_content 消息报文
     * sign	 签名
     * sign_type 签名类型
     * encrypt_type	加密算法
     * charset	编码集，该字符集为验签和解密所需要的字符集
     * notify_type	[1.0版本老协议参数]通知类型，1.1接口没有该参数
     * notify_time	[1.0版本老协议参数]通知时间，北京时区，时间格式为：yyyy-MM-dd HH:mm:ss，如果服务器部署在国外请注意做时区转换。若version=1.1则可以使用utc_timestamp识别时间
     * auth_app_id	[1.0版本老协议参数]授权方的应用id
     */
    @RequestMapping(value = "/alipaySuccessInfo", method = RequestMethod.GET)
    @ApiOperation(value = "直付通/服务商二级商户进件审核通过消息")
    public String alipaySuccessInfo(@RequestParam("charset") String charset,
                                    @RequestParam("biz_content") String biz_content,
                                    @RequestParam("msg_method") String msg_method,
                                    @RequestParam("utc_timestamp") String utc_timestamp,
                                    @RequestParam("version") String version,
                                    @RequestParam("sign_type") String sign_type,
                                    @RequestParam("notify_id") String notify_id,
                                    @RequestParam("app_id") String app_id,
                                    @RequestParam("sign") String sign,
                                    @RequestParam(value = "msg_type", required = false) String msg_type,
                                    @RequestParam(value = "msg_app_id", required = false) String msg_app_id,
                                    @RequestParam(value = "encrypt_type", required = false) String encrypt_type,
                                    @RequestParam(value = "notify_type", required = false) String notify_type,
                                    @RequestParam(value = "notify_time", required = false) String notify_time,
                                    @RequestParam(value = "auth_app_id", required = false) String auth_app_id) {

        return zftSimpleCreateService.alipaySuccessInfo(charset, biz_content, msg_app_id, utc_timestamp, version, sign_type, notify_type, notify_id, app_id, sign, msg_method, msg_type, encrypt_type, notify_time, auth_app_id);
    }

    /**
     *  ant.merchant.expand.indirect.zft.rejected(直付通/服务商二级商户进件拒绝消息)
     * notify_id	通知ID
     * utc_timestamp 消息发送时的服务端时间
     * msg_method	消息接口名称
     * app_id	消息接受方的应用id
     * msg_type	消息类型。目前支持类型：sys：系统消息；usr，用户消息；app，应用消息
     * msg_app_id 消息归属方的应用id。应用消息时非空
     * version	 版本号(1.1版本为标准消息)
     * biz_content 消息报文
     * sign	 签名
     * sign_type 签名类型
     * encrypt_type	加密算法
     * charset	编码集，该字符集为验签和解密所需要的字符集
     * notify_type	[1.0版本老协议参数]通知类型，1.1接口没有该参数
     * notify_time	[1.0版本老协议参数]通知时间，北京时区，时间格式为：yyyy-MM-dd HH:mm:ss，如果服务器部署在国外请注意做时区转换。若version=1.1则可以使用utc_timestamp识别时间
     * auth_app_id	[1.0版本老协议参数]授权方的应用id
     */
    @RequestMapping(value = "/alipayFailInfo", method = RequestMethod.GET)
    @ApiOperation(value = "直付通/服务商二级商户进件拒绝消息")
    public String alipayFailInfo(@RequestParam("charset") String charset,
                                 @RequestParam("biz_content") String biz_content,
                                 @RequestParam("msg_method") String msg_method,
                                 @RequestParam("utc_timestamp") String utc_timestamp,
                                 @RequestParam("version") String version,
                                 @RequestParam("sign_type") String sign_type,
                                 @RequestParam("notify_id") String notify_id,
                                 @RequestParam("app_id") String app_id,
                                 @RequestParam("sign") String sign,
                                 @RequestParam(value = "msg_type", required = false) String msg_type,
                                 @RequestParam(value = "msg_app_id", required = false) String msg_app_id,
                                 @RequestParam(value = "encrypt_type", required = false) String encrypt_type,
                                 @RequestParam(value = "notify_type", required = false) String notify_type,
                                 @RequestParam(value = "notify_time", required = false) String notify_time,
                                 @RequestParam(value = "auth_app_id", required = false) String auth_app_id) {

        return zftSimpleCreateService.alipayFailInfo(charset, biz_content, msg_app_id, utc_timestamp, version, sign_type, notify_type, notify_id, app_id, sign, msg_method, msg_type, encrypt_type, notify_time, auth_app_id);
    }
    //</editor-fold>




}
